package org.redmagic.bulk;

import java.io.IOException;
import java.util.Set;

import org.redmagic.bulk.BulkProperty.Serializer;
import org.redmagic.common.MagicProperty;
import org.redmagic.domain.InternalServiceProperty;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

@JsonSerialize(using=Serializer.class)
public class BulkProperty extends MagicProperty {
	
	public static BulkProperty fromUiProperty(InternalServiceProperty property) {
		return new BulkProperty(property.getName(), property.getValue(), property.getEncoding(), property.getTags());
	}

	private final Set<String> tags;

	private BulkProperty(String name, String value, String encoding, Set<String> tags) {
		super(name, value, encoding);
		this.tags = tags;
	}
	
	public Set<String> getTags() {
		return tags;
	}
	
	protected static class Serializer extends JsonSerializer<BulkProperty> {
		@Override
		public void serialize(BulkProperty property, JsonGenerator jgen,
				SerializerProvider provider) throws IOException,
				JsonProcessingException {
			
			jgen.writeStartObject();
			jgen.writeStringField("name", property.getName());
			jgen.writeStringField("value", property.getValue());
			jgen.writeStringField("encoding", property.getEncoding());
			jgen.writeEndObject();
		}
	}
}
